package com.wu.advapplication.servise;

import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import com.wu.advapplication.MainActivity;
import com.wu.advapplication.utils.BaseUtil;

import java.util.Timer;
import java.util.TimerTask;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

/**
 * 前台Service保活
 */
public class ForegroundService extends Service {
    private static final String TAG = "ForegroundService";
    private static final int SERVICE_ID = 1;
    private final Timer timerMail = new Timer();
    private long exitTime;
    private int r = 0;
    private SharedPreferences preferences;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "ForegroundService 创建了");
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {   //4.3以下
            startForeground(SERVICE_ID, new Notification());
        } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {//4.3-7.0
            startForeground(SERVICE_ID, new Notification());
            startService(new Intent(this, InnerService.class));//通过启动一个inner服务隐藏通知栏
        } else {    //8.0以上
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel("wanandroid", "wanAndroid", NotificationManager.IMPORTANCE_LOW);
            if (manager != null) {
                manager.createNotificationChannel(channel);
                Notification notification = new NotificationCompat.Builder(this, "wanandroid").build();
                startForeground(SERVICE_ID, notification);
            }
        }

        //监听act是否在打开
        preferences = this.getSharedPreferences("ADV_APP", 0);
        timerMail.schedule(new TimerTask() {
            @Override
            public void run() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                    //app没有打开
                    if (!BaseUtil.isBackgroundRunning(getApplication())) {
                        r = 0;
                        Intent intent = new Intent(getApplication(), MainActivity.class);
                        intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES | Intent.FLAG_ACTIVITY_NEW_TASK);
                        getApplication().startActivity(intent);
                    } else {
                        //1分钟后如果用户还没打开权限再弹出一次
                        if ((System.currentTimeMillis() - exitTime) > 1000 * 30 && r < 3) {
                            r++;
                            exitTime = System.currentTimeMillis();
                            if (!BaseUtil.getTopApp(getApplication(), MainActivity.class)) {
                                Log.i(TAG, "将app设置前台显示");
                                ActivityManager am = (ActivityManager) getApplication().getSystemService(Context.ACTIVITY_SERVICE);
                                am.moveTaskToFront(BaseUtil.getActivityTaskId(getApplication(), MainActivity.class), ActivityManager.MOVE_TASK_WITH_HOME);
                            }
                        }
                    }


                }
            }
        }, 0, 1000);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        startService(new Intent(this, ForegroundService.class));
    }

    public static class InnerService extends Service {

        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onCreate() {
            super.onCreate();
            Log.e(TAG, "InnerService 创建了");
            startForeground(SERVICE_ID, new Notification());
            stopSelf();
        }
    }

}
